1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
import ReactMarkdown from 'react-markdown';
import { readdirSync, readFileSync } from 'fs';
import { join } from 'path';
import Seperator from '../../components/seperator';
import Navbar from '../../components/navbar';
// import Button from '../../components/button';
import Image from '../../components/image';
import Chapters, { chapter } from '../../components/chapters';
interface ArticleMeta {
title?: string;
subtitle?: string;
author?: string;
date?: string;
chapters?: Array<chapter>;
}
export default function Post(props: {
content: string,
meta: ArticleMeta
}) {
return <div>
<div className="centeredPage">
<div className="titleWrapper">
<h1>{props.meta.title}</h1>
<p className="subtile">{props.meta.subtitle}</p>
</div>
<div className="navAreaWrapper">
<div className="sticky">
<Navbar/>
<Chapters chapters={props.meta.chapters}/>
</div>
</div>
<div className="contentWrapper">
<ReactMarkdown
children={props.content}
renderers={{
image: Image,
thematicBreak: Seperator,
}}/>
</div>
</div>
</div>
}
var parseTag = {
"title": (val: string) => val,
"subtitle": (val: string) => val,
"author": (val: string) => val,
"date": (val: string) => new Date(val).toDateString(),
}
function parseMeta(file: Array<string>) {
var meta: ArticleMeta = {};
file.forEach(line => {
if (!line.startsWith("[meta]: ")) return;
var tags = line.match(/\[meta\]:\s+\<(.+?)\>\s+\((.+?)\)/);
if (!tags || !tags[1] || !tags[2]) return;
if (!parseTag.hasOwnProperty(tags[1])) return;
meta[tags[1]] = parseTag[tags[1]](tags[2]);
});
return meta;
}
function preprocessor(fileContent: string) {
var fileAsArr = fileContent.split("\n");
var meta = parseMeta(fileAsArr);
var result = fileAsArr.join("\n").trim()
return { meta, result }
}
export function getStaticProps(props: {params: { id: string }}) {
var filename = join("posts/", props.params.id + ".md")
var filecontent = readFileSync(filename).toString().trim()
var parsed = preprocessor(filecontent);
return {
props: {
content: parsed.result,
meta: parsed.meta,
},
}
}
export function getStaticPaths() {
var files = readdirSync("posts").filter(f => f.endsWith(".md"));
return {
paths: files.map((f) => {
return {
params: {
id: f.substr(0, f.length - 3)
}
}
}),
fallback: false,
}
}
|